package com.wei;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author :sunwei
 * @Description:
 * @Date create in 17:17  2021/4/25
 * @Modified by:
 */
public class MyHandler implements InvocationHandler {

    private ObjectInterface target;
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object returnValue = method.invoke(this.target, args);
        after();
        return returnValue;
    }

    void after(){
        System.out.println("do after things...");
    }

    void before(){
        System.out.println("do before things...");
    }

    public ObjectInterface getInstance(ObjectInterface target){
        this.target=target;
        Class<? extends ObjectInterface> clazz = target.getClass();
        return (ObjectInterface)Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }
}
